package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Module;
import com.itheima.domain.Result;
import com.itheima.domain.User;
import com.itheima.factory.BeanFactory;
import com.itheima.service.ModuleService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/system/module/*")
public class ModuleServlet extends BaseServlet {
    ModuleService moduleService = BeanFactory.getBean("moduleService",ModuleService.class);
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //2 调用service层方法，查询所有数据
            List<Module> list = moduleService.findAll();
            //3 响应结果
            return new Result(true, MessageConstant.DELETE_MODULE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MODULE_FAIL);
        }
    }

    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            //调用service层edit方法,执行删除操作
            moduleService.delete(ids);
            //响应客户端
            return new Result(true,MessageConstant.DELETE_MODULE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_MODULE_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            Module module = BeanUtil.fillBeanFromJson(request, Module.class);
            //调用service层edit方法,执行保存操作
            moduleService.edit(module);
            //响应客户端
            return new Result(true,MessageConstant.EDIT_MODULE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_MODULE_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1.获取请求参数
            String moduleId = request.getParameter("id");
            //调用service层方法,获取查询结果
            Module module = moduleService.findById(moduleId);
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_MODULE_SUCCESS,module);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MODULE_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            Module module = BeanUtil.fillBeanFromJson(request, Module.class);
            //调用service层save方法,执行保存操作
            moduleService.save(module);
            //响应客户端
            return new Result(true,MessageConstant.SAVE_MODULE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.SAVE_MODULE_FAIL);
        }
    }

    //处理分页查询的请求
    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //1.获取请求参数
            String strCurrentPage = request.getParameter("currentPage");
            String strPageSize = request.getParameter("pageSize");
            //1.1判断非空
            int currentPage = 1,pageSize = 5;
            if(!StringUtils.isBlank(strCurrentPage)){
                currentPage = Integer.parseInt(strCurrentPage);
            }
            if(!StringUtils.isBlank(strPageSize)){
                pageSize = Integer.parseInt(strPageSize);
            }
            //调用service层方法,获取查询结果
            PageInfo<Module> pageInfo = moduleService.findByPage(currentPage,pageSize);
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_MODULE_SUCCESS,pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MODULE_FAIL);
        }
    }

    //查询角色权限
    private Result findModulesByRoleId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1.获取请求参数
            String roleId = request.getParameter("roleId");
            //调用service层方法,获取查询结果
            List<Map<String,Object>> list = moduleService.findModulesByRoleId(roleId);
            //对客户端做出响应
            return new Result(true, MessageConstant.LOAD_ZTREE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.LOAD_ZTREE_FAIL);
        }
    }

    //查询登录的用户拥有的菜单列表
    private Result findMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //获取session中的user对象
            User user = (User) request.getSession().getAttribute("user");
            //调用service方法,获取该用户具有的菜单列表
            List<Module> list = moduleService.findMenu(user.getId());
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
}
